﻿Imports Microsoft.VisualBasic
Imports JPC.Medialibrary.BLL

Namespace JPC.Medialibrary.UI

    Public Class BreadcrumbList
        Inherits List(Of ListItem)
    End Class

    Public Class Breadcrumb

        Public Shared Sub ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs)

            If (e.Item.ItemType = ListViewItemType.DataItem) Then
                Dim btn As LinkButton = CType(e.Item.FindControl("btnBreadcrumb"), LinkButton)
                Dim dataItem As Object = CType(e.Item, ListViewDataItem).DataItem
                Dim bci As ListItem = CType(dataItem, ListItem)
                btn.CommandArgument = bci.Index.ToString
                btn.Text = bci.Text
            End If

        End Sub

        Public Shared Sub RemoveRange(ByVal index As Integer)

            MySession.BreadCrumbList.RemoveRange(index, MySession.BreadCrumbList.Count - index)
            MySession.LastBreadcrumb = Nothing

        End Sub

        Public Shared Sub AddItem(ByVal Text As String, ByVal CommandName As String, ByVal CommandArgument As String)

            'Si el argumento es -1 estoy recibiendo un item del menú, así que borro todo
            'Si el argumento es "" estoy mostrando la lista de series
            If CommandArgument = "-1" Or CommandArgument = "" Then
                MySession.LastBreadcrumb = Nothing
                MySession.BreadCrumbList = Nothing
                AddItem("Inicio", "Inicio", "0")
            End If

            If MySession.BreadCrumbList Is Nothing Then
                MySession.BreadCrumbList = New BreadcrumbList
            End If
            If MySession.LastBreadcrumb Is Nothing Then
                MySession.LastBreadcrumb = New ListItem
            End If

            'El último link se agrega al final de la lista
            If MySession.LastBreadcrumb.Text <> "" Then
                MySession.LastBreadcrumb.Index = MySession.BreadCrumbList.Count
                MySession.BreadCrumbList.Add(MySession.LastBreadcrumb)
            End If

            'y el nuevo link se transforma en el último
            Dim bci As New ListItem
            bci.CommandName = CommandName
            bci.Text = Text
            bci.CommandArgument = CommandArgument
            If CommandName = "Inicio" Then
                MySession.BreadCrumbList.Add(bci)
            Else
                MySession.LastBreadcrumb = bci
            End If

            If CommandName.IndexOf("By") >= 0 Then
                bci.ObjectType = CommandName.Substring(CommandName.IndexOf("By") + 2)
            End If

        End Sub

        Public Shared Sub Bind(ByVal lblLastBreadcrumb As Label, ByVal lstBreadCrumb As ListView)

            If Not MySession.BreadCrumbList Is Nothing And Not MySession.LastBreadcrumb Is Nothing Then
                lblLastBreadcrumb.Text = MySession.LastBreadcrumb.Text
                lstBreadCrumb.DataSource = MySession.BreadCrumbList
                lstBreadCrumb.DataBind()
            End If

        End Sub

        Public Shared Sub Previous(ByVal cantidad As Integer, ByRef text As String, ByRef commandArgument As String, ByRef commandName As String)

            text = MySession.BreadCrumbList.Item(MySession.BreadCrumbList.Count - cantidad).Text
            commandArgument = MySession.BreadCrumbList.Item(MySession.BreadCrumbList.Count - cantidad).CommandArgument
            commandName = MySession.BreadCrumbList.Item(MySession.BreadCrumbList.Count - cantidad).CommandName
            MySession.BreadCrumbList.RemoveRange(MySession.BreadCrumbList.Count - cantidad, cantidad)
            MySession.LastBreadcrumb = Nothing

        End Sub

    End Class

End Namespace
